package com.benmei.weike.service;

import com.benmei.weike.common.Constants;
import com.benmei.weike.dao.*;
import com.benmei.weike.dto.CourseExchangeDto;
import com.benmei.weike.dto.ExchangeCourseDto;
import com.benmei.weike.dto.MembReturnCourseListDto;
import com.benmei.weike.entity.InviteBackClass;
import com.benmei.weike.entity.Student;
import com.benmei.weike.entity.TdNtMembExchangeRecord;
import com.benmei.weike.util.PicUnion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MemberReturnCourseService {

    @Autowired
    private InviteBackClassDao inviteBackClassDao;
    @Autowired
    private MembExchangeRecordDao membExchangeRecordDao;
    @Autowired
    private WebsiteConfigDao websiteConfigDao;
    @Autowired
    private CourseInfoDao courseInfoDao;
    @Autowired
    private StudentDao studentDao;

    @Value("${share.register.url}")
    public String registerUrl;

    private static final String PHONO_PREFIX = "http://file.waijiaojun.com/";

    private static Logger logger = LoggerFactory.getLogger(MemberReturnCourseService.class);

    public MembReturnCourseListDto ReturnCourseList(Integer memb_id) {
        MembReturnCourseListDto membReturnCourseListDto = new MembReturnCourseListDto();
        membReturnCourseListDto.setRole_introduction(websiteConfigDao.getData(38));
        List<InviteBackClass> inviteBackClassList = new ArrayList<>();
        inviteBackClassList = inviteBackClassDao.selectBackClassList(memb_id);
        membReturnCourseListDto.setInvite_used_count(membExchangeRecordDao.selectInvateCount(memb_id));
        if (inviteBackClassList != null) {
            membReturnCourseListDto.setInvite_member_count(inviteBackClassList.size());
            membReturnCourseListDto.setInvite_course_count(inviteBackClassList.size() * Integer.parseInt(websiteConfigDao.getData(37)));
            membReturnCourseListDto.setAll_price(BigDecimal.valueOf(inviteBackClassList.size() * Integer.parseInt(websiteConfigDao.getData(37)) * courseInfoDao.getByIdSetmealTrialPrice().intValue()));
        }
        membReturnCourseListDto.setInviteBackClassList(inviteBackClassList);

        return membReturnCourseListDto;
    }

    public Map<String, Object> ReturnExchangeCourseList(Integer memb_id) {
        Map<String, Object> map = new HashMap<>();
        Integer course_can_use = 0;
        Integer course_used = 0;
        Integer a = membExchangeRecordDao.selectInvateCount(memb_id);
        if (a != null) {
            course_used = a;
        }
        Integer course_count = inviteBackClassDao.selectBackClassListCount(memb_id) * Integer.parseInt(websiteConfigDao.getData(37));
        course_can_use = course_count - course_used;
        List<ExchangeCourseDto> exchangeCourseDto = courseInfoDao.getCourseCanExchange(memb_id);
        map.put("exchangeCourseList", exchangeCourseDto);
        map.put("course_can_use", course_can_use);
        return map;
    }

    @Transactional
    public Map<String, Object> ReturnExchangeCourse(Integer memb_id, CourseExchangeDto courseExchangeDto) {
        Map<String, Object> map = new HashMap<>();
        Integer course_can_use = 0;
        Integer course_used = 0;
        Integer a = membExchangeRecordDao.selectInvateCount(memb_id);
        if (a != null) {
            course_used = a;
        }
        Integer course_count = inviteBackClassDao.selectBackClassListCount(memb_id) * Integer.parseInt(websiteConfigDao.getData(37));
        course_can_use = course_count - course_used;
        //兑换课程数小于等于可兑换课时数
        if (courseExchangeDto.getExchange_count() <= course_can_use) {
            //兑换课程(更新套餐课时数，可用课时数，到期时间)
            courseInfoDao.exchangeCourse(memb_id, courseExchangeDto.getMemb_set_id(), courseExchangeDto.getExchange_count());
            //添加到兑换记录表中
            TdNtMembExchangeRecord tdNtMembExchangeRecord = new TdNtMembExchangeRecord();
            tdNtMembExchangeRecord.setMemb_id(memb_id);
            tdNtMembExchangeRecord.setMemb_set_id(courseExchangeDto.getMemb_set_id());
            tdNtMembExchangeRecord.setCourse_size(courseExchangeDto.getExchange_count());
            membExchangeRecordDao.insertExchangeCourse(tdNtMembExchangeRecord);
            course_can_use = course_can_use - courseExchangeDto.getExchange_count();
        }
        List<ExchangeCourseDto> exchangeCourseDto = courseInfoDao.getCourseCanExchange(memb_id);
        map.put("exchangeCourseList", exchangeCourseDto);
        map.put("course_can_use", course_can_use);
        return map;
    }

    public Map<String, Object> ReturnPicUrl(Integer memb_id, String app_type) {
        Map<String, Object> map = new HashMap<>();
        map.put("text", websiteConfigDao.getData(41));
        map.put("protocol_url", "http://app.a.student.nativetalk.waijiaojun.com/article/detail?arti_id=1");
        map.put("image_url", "");
        Student student = studentDao.getById(memb_id);
        String invite_code = student.getMemb_invite_code();
        String url = registerUrl + "?invite_code=" + invite_code;
        String image_url;
        if (Constants.AppType.A.equalsIgnoreCase(app_type)) {
            image_url = PicUnion.mergeA_InvitationCard(memb_id, url);// 生成我的邀请卡图片，成人版
        } else {
            image_url = PicUnion.mergeTeenagerInvitationCard(memb_id, url);// 生成我的邀请卡图片，青少版
        }
        map.put("image_url", image_url);
        return map;
    }

}
